; winner.asm
; displays the player number of the winner

	.include "global.inc"
	
	.export show_winner
	
	numptr = tmpptr1
	
	.segment "CODE"
	
show_winner: ; Y ... player number (0..8)
			
			; init ROM charset & RAM positions
			lda VIC_BASE+2
			and #$7F
			sta VIC_BASE+2
			lda VIC_BASE+5
			and #$F0
			sta VIC_BASE+5
			
			; clear screen
			ldx #0
@clrloop:	lda #$20
			sta VIDEO_BASE, x
			sta VIDEO_BASE+$0100, x
			lda #0
			sta COLOR_BASE, x
			sta COLOR_BASE+$0100, x
			inx
			bne @clrloop
			
			; display text
			ldx #22*5-1
@plrlp:		lda txplayer, x
			sta VIDEO_BASE + 22*2, x
			dex
			bpl @plrlp
			
			lda txnumlo, y				; calc digit address
			sta numptr
			lda txnumhi, y
			sta numptr+1
			
			lda #<(VIDEO_BASE + 22*(2+7) + 9)
			sta @scrptr
			lda #>(VIDEO_BASE + 22*(2+7) + 9)
			sta @scrptr+1
			
			ldy #0
@outlp:		ldx #0
@inlp:		lda (numptr), y
@cpadr:		sta $F000, x
	@scrptr = @cpadr+1
			inx
			iny
			cpx #3
			bcc @inlp
			cpy #15
			bcs @shwin
			lda @scrptr
			adc #22
			sta @scrptr
			lda @scrptr+1
			adc #0
			sta @scrptr+1
			bne @outlp			
			
@shwin:		ldx #15-1
@winlp:		lda txwins1, x
			sta VIDEO_BASE + 22*(2+14) + 3, x
			lda txwins2, x
			sta VIDEO_BASE + 22*(3+14) + 3, x
			lda txwins3, x
			sta VIDEO_BASE + 22*(4+14) + 3, x
			lda txwins4, x
			sta VIDEO_BASE + 22*(5+14) + 3, x
			lda txwins5, x
			sta VIDEO_BASE + 22*(6+14) + 3, x
			dex
			bpl @winlp
			rts

	.segment "RODATA"
	
txplayer:	.byte $A0, $A0, $DF, $20, $A0, $20, $20, $E9, $A0, $DF, $20, $A0, $20, $A0, $20, $A0, $A0, $A0, $20, $A0, $A0, $DF
			.byte $A0, $20, $A0, $20, $A0, $20, $20, $A0, $20, $A0, $20, $A0, $20, $A0, $20, $A0, $20, $20, $20, $A0, $20, $A0
			.byte $A0, $A0, $69, $20, $A0, $20, $20, $A0, $A0, $A0, $20, $5F, $A0, $69, $20, $A0, $A0, $20, $20, $A0, $A0, $69
			.byte $A0, $20, $20, $20, $A0, $20, $20, $A0, $20, $A0, $20, $20, $A0, $20, $20, $A0, $20, $20, $20, $A0, $DF, $20
			.byte $A0, $20, $20, $20, $A0, $A0, $20, $A0, $20, $A0, $20, $20, $A0, $20, $20, $A0, $A0, $A0, $20, $A0, $5F, $DF

txwins1:	.byte $A0, $20, $A0, $20, $A0, $20, $A0, $20, $A0, $20, $A0, $20, $E9, $A0, $DF
txwins2:	.byte $A0, $20, $A0, $20, $A0, $20, $A0, $20, $A0, $DF, $A0, $20, $A0, $20, $20
txwins3:	.byte $A0, $20, $A0, $20, $A0, $20, $A0, $20, $A0, $A0, $A0, $20, $5F, $A0, $DF
txwins4:	.byte $A0, $20, $A0, $20, $A0, $20, $A0, $20, $A0, $5F, $A0, $20, $20, $20, $A0
txwins5:	.byte $5F, $A0, $A0, $A0, $69, $20, $A0, $20, $A0, $20, $A0, $20, $5F, $A0, $69

txnum1:		.byte $E9, $A0, $20,  $69, $A0, $20,  $20, $A0, $20,  $20, $A0, $20,  $A0, $A0, $A0
txnum2:		.byte $E9, $A0, $DF,  $A0, $20, $A0,  $20, $E9, $69,  $E9, $69, $20,  $A0, $A0, $A0
txnum3:		.byte $E9, $A0, $DF,  $20, $E9, $69,  $20, $A0, $20,  $20, $5F, $DF,  $5F, $A0, $69
txnum4:		.byte $A0, $20, $A0,  $A0, $20, $A0,  $A0, $A0, $A0,  $20, $20, $A0,  $20, $20, $A0
txnum5:		.byte $A0, $A0, $A0,  $A0, $20, $20,  $A0, $A0, $DF,  $20, $20, $A0,  $5F, $A0, $69
txnum6:		.byte $E9, $A0, $DF,  $A0, $20, $20,  $A0, $A0, $DF,  $A0, $20, $A0,  $5F, $A0, $69
txnum7:		.byte $A0, $A0, $A0,  $20, $e9, $69,  $20, $A0, $20,  $20, $A0, $20,  $20, $A0, $20
txnum8:		.byte $E9, $A0, $DF,  $A0, $20, $A0,  $A0, $A0, $A0,  $A0, $20, $A0,  $5F, $A0, $69
txnum9:		.byte $E9, $A0, $DF,  $A0, $20, $A0,  $5F, $A0, $A0,  $20, $20, $A0,  $5F, $A0, $69

txnumlo:	.byte <txnum1, <txnum2, <txnum3, <txnum4, <txnum5, <txnum6, <txnum7, <txnum8, <txnum9
txnumhi:	.byte >txnum1, >txnum2, >txnum3, >txnum4, >txnum5, >txnum6, >txnum7, >txnum8, >txnum9
